package com.keldian.mobilesafe.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.activity.HomeActivity;
import com.keldian.mobilesafe.utils.MD5Utils;
import com.keldian.mobilesafe.utils.SharePrefenceTool;

/**
 * Created by Administrator on 15-12-11.
 */
public class DialogSetPwd extends AlertDialog {
    private Context context;

    private EditText et_setup_pwd;
    private EditText et_setup_confirm;
    private Button ok;
    private Button cancel;
    private AlertDialog dialog;

    public DialogSetPwd(Context context) {
        super(context);
        this.context = context;
        showDialog();
    }

    public DialogSetPwd(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void showDialog() {
        AlertDialog.Builder builder = new Builder(context);
        View view = View.inflate(context, R.layout.dialog_setup_password, null);
        et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
        et_setup_confirm = (EditText) view.findViewById(R.id.et_setup_confirm);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "取消", Toast.LENGTH_LONG).show();
                //把这个对话框取消掉
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //  取出密码
                String password = et_setup_pwd.getText().toString().trim();
                String password_confirm = et_setup_confirm.getText().toString().trim();
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(password_confirm)) {
                    Toast.makeText(context, "密码为空", Toast.LENGTH_LONG).show();
                    return;
                }

                if (password.equals(password_confirm)) {
                    Toast.makeText(context, "Set OK", Toast.LENGTH_LONG).show();
                    SharePrefenceTool.setFDPwd(context, MD5Utils.md5Password(password));
                    dialog.dismiss();
                }
            }
        });
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }
}
